//
//  CalendarViewController.m
//  PCSchool
//
//  Created by Arindam Ghosh on 7/12/13.
//  Copyright (c) 2013 Jayanta. All rights reserved.
//

#import "CalendarViewController.h"
#import <CoreGraphics/CoreGraphics.h>
#import "CKCalendarView.h"

@interface CalendarViewController () <CKCalendarDelegate>

@property(nonatomic, assign) CKCalendarView *calendar;
@property(nonatomic, strong) UILabel *dateLabel;
@property(nonatomic, strong) NSDateFormatter *dateFormatter;
@property(nonatomic, strong) NSDate *minimumDate;
@property(nonatomic, strong) NSArray *disabledDates;

@end

@implementation CalendarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init {
    self = [super init];
    if (self) {
        CKCalendarView *calendar = [[CKCalendarView alloc] initWithStartDay:startMonday];
        self.calendar = calendar;
        calendar.delegate = self;
        
        self.dateFormatter = [[NSDateFormatter alloc] init];
        [self.dateFormatter setDateFormat:@"dd/MM/yyyy"];
        self.minimumDate = [self.dateFormatter dateFromString:@"20/09/2012"];
        
        self.disabledDates = @[
                               [self.dateFormatter dateFromString:@"05/01/2013"],
                               [self.dateFormatter dateFromString:@"06/01/2013"],
                               [self.dateFormatter dateFromString:@"07/01/2013"]
                               ];
        
        calendar.onlyShowCurrentMonth = NO;
        calendar.adaptHeightToNumberOfWeeksInMonth = YES;
        
        calendar.frame = CGRectMake(460, 80, 403, 20);
        [self.view addSubview:calendar];
        
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(calendar.frame) + 4, self.view.bounds.size.width, 24)];
        //[self.view addSubview:self.dateLabel];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localeDidChange) name:NSCurrentLocaleDidChangeNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.rightPanelTableView.scrollEnabled = NO;

}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSLog(@"%@",NSStringFromClass(self.class));
    
    self.studentArray = [NSMutableArray array];

    
    [self.studentArray addObject:@"Adam 1"];
    [self.studentArray addObject:@"Adam 2"];
    [self.studentArray addObject:@"Adam 3"];
    [self.studentArray addObject:@"Adam 4"];
    [self.studentArray addObject:@"Adam 5"];
    [self.studentArray addObject:@"Adam 6"];
    [self.studentArray addObject:@"Adam 7"];
    [self.studentArray addObject:@"Adam 8"];
    [self.studentArray addObject:@"Adam 9"];
    [self.studentArray addObject:@"Adam 10"];
    [self.studentArray addObject:@"Adam 11"];
    [self.studentArray addObject:@"Adam 12"];
    [self.studentArray addObject:@"Adam 13"];
    [self.studentArray addObject:@"Adam 14"];
    [self.studentArray addObject:@"Adam 15"];
}

- (void)viewDidAppear:(BOOL)animated {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.leftPanelTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (void)localeDidChange {
    [self.calendar setLocale:[NSLocale currentLocale]];
}

- (BOOL)dateIsDisabled:(NSDate *)date {
    for (NSDate *disabledDate in self.disabledDates) {
        if ([disabledDate isEqualToDate:date]) {
            return YES;
        }
    }
    return NO;
}


#pragma mark -
#pragma mark - CKCalendarDelegate

- (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date {
    // TODO: play with the coloring if we want to...
    if ([self dateIsDisabled:date]) {
        dateItem.backgroundColor = [UIColor redColor];
        dateItem.textColor = [UIColor whiteColor];
    }
}

- (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date {
    return ![self dateIsDisabled:date];
}

- (void)calendar:(CKCalendarView *)calendar didSelectDate:(NSDate *)date {
    self.dateLabel.text = [self.dateFormatter stringFromDate:date];
    NSLog(@"%@",[self.dateFormatter stringFromDate:date]);
}

- (BOOL)calendar:(CKCalendarView *)calendar willChangeToMonth:(NSDate *)date {
    if ([date laterDate:self.minimumDate] == date) {
        //self.calendar.backgroundColor = [UIColor blueColor];
        return YES;
    } else {
        //self.calendar.backgroundColor = [UIColor redColor];
        return NO;
    }
}

- (void)calendar:(CKCalendarView *)calendar didLayoutInRect:(CGRect)frame {
    NSLog(@"calendar layout: %@", NSStringFromCGRect(frame));
}



#pragma mark - TableView Delegates & Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        return 46.0;
    }
    if (tableView.tag == 102) {
        
        return 55.0;
    }
    
    return 0.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 101) {
        return [self.studentArray count];
        
    }
    if (tableView.tag == 102) {
        return 4;
        
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        static NSString *CellIdentifier = @"StudenListCell";
        StudenListCell * cell =(StudenListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(StudenListCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            
        }
        
        [self configureCell:cell atIndexPath:indexPath];
        
        return cell;
    }
    if (tableView.tag == 102) {
        
        static NSString *CellIdentifier = @"SubjectTableCell";
        SubjectTableCell * cell =(SubjectTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(SubjectTableCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self configureCell2:cell atIndexPath:indexPath];
        
        
        
        return cell;
    }
    
    return nil;
    
}

-(void)configureCell:(StudenListCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellTextLabel.text = [self.studentArray objectAtIndex:indexPath.row];
}

-(void)configureCell2:(SubjectTableCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellLeftLabel.text = [self.studentArray objectAtIndex:indexPath.row];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - Button Action

- (IBAction)proximityButtonAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    
    int selectedIndex = btn.selected ? 0 : 1;
    
    btn.selected = selectedIndex;
}



#pragma mark - Memory CleanUp

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_baseImageView release];
    [_rightPanelImageView release];
    [_leftPanelView release];
    [_leftPanelImageView release];
    [_leftPanelTableView release];
    [_searchBarImgView release];
    [_searchBarTextField release];
    [_proximityButton release];
    [_searchBarLeftimageView release];
    
    [_studentArray release];
    [_rightPanelTableView release];
    
    [super dealloc];
}

@end
